import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import Picker from 'vux/src/components/picker/index.vue'
import Popup from 'vux/src/components/popup/index.vue'
import TransferDom from 'vux/src/directives/transfer-dom/index'
import XInput from 'vux/src/components/x-input/index.vue'
import Group from 'vux/src/components/group/index.vue'

export default {
  name: 'oneToOneScreen',
  components: {Picker, Popup,XInput,Group},
  directives: {
    TransferDom
  },
  data () {
    return {
      showdy: false,
      showxb: false,
      shownl: false,
      area: [],
      aready: [],
      sexs: ['不限', '男', '女'],
      sex: [],
      ages: ['不限', '12', '13', '14'],
      age: [],
      name: '',
      school: '',
      biunique_id: '',
      title: '',
      peopleNum: '',
      pageNo: 1,
      pageSize: 20,
      studentList: []
    }
  },
  mounted () {
    if (this.$emit('ifOpenid')) {
      this.$emit('ifOpenid', this.$route.path)
    }
    this.biunique_id = this.$route.query.event_id
    http.post(CONSTANT.SCREEN, {biunique_id: this.biunique_id}, (res) => {
      console.log(res)
      if (res.code == 200) {
        this.title = res.obj.title
        this.peopleNum = res.obj.needNumber
        this.pageSize = this.peopleNum
        var ageArr = ['不限']
        var sexArr = ['不限']
        var areaArr = [{name: '不限', value: '不限', parent: 0}, {name: '', value: '', parent: '不限'}]
        res.obj.succourAge.sort(function (a, b) {
          return a - b
        })
        for (var a = 0; a < res.obj.succourAge.length; a++) {
          ageArr.push(res.obj.succourAge[a])
        }
        for (var s = 0; s < res.obj.succourSex.length; s++) {
          if (res.obj.succourSex[s] == 'woman') {
            res.obj.succourSex[s] = '女'
          } else if (res.obj.succourSex[s] == 'man') {
            res.obj.succourSex[s] = '男'
          }
          sexArr.push(res.obj.succourSex[s])
        }
        for (var succour = 0; succour < res.obj.succourArea.length; succour++) {
          for (var shiji in res.obj.succourArea[succour]) {
            var sjdui = {}
            sjdui.name = shiji
            sjdui.value = shiji
            sjdui.parent = 0
            areaArr.push(sjdui)
            for (var xianji = 0; xianji < res.obj.succourArea[succour][shiji].length; xianji++) {
              var xjdui = {}
              xjdui.name = res.obj.succourArea[succour][shiji][xianji]
              xjdui.value = res.obj.succourArea[succour][shiji][xianji]
              xjdui.parent = shiji
              areaArr.push(xjdui)
            }
          }
        }
        this.area = areaArr
        this.ages = ageArr
        this.sexs = sexArr
      }
    })
  },
  methods: {
    goScreen () {
      var city = ''
      var country = ''
      var sexS = ''
      var ageS = ''
      if (this.aready[0] == '不限') {
        city = ''
        country = ''
      } else {
        city = this.aready[0]
        country = this.aready[1]
      }
      if (this.sex[0] == '不限') {
        sexS = ''
      } else if (this.sex[0] == '男') {
        sexS = 'man'
      } else if (this.sex[0] == '女') {
        sexS = 'woman'
      }
      if (this.age[0] == '不限') {
        ageS = ''
      } else {
        ageS = this.age[0] * 1
      }
      http.post(CONSTANT.SEARCH_STUDENT, {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        age: ageS,
        city: city,
        country: country,
        sex: sexS,
        name: this.name,
        school: this.school,
        biunique_id: this.biunique_id
      }, (res) => {
        console.log(res)
        if (res.code == 200) {
          if (res.errmsg == '没有更多的学生') {
            this.studentList = []
            alert('未筛选到匹配对象，请重新选择！')
            return
          }
          this.studentList = res.list
          for (var stu = 0; stu < this.studentList.length; stu++) {
            var names = this.studentList[stu].name
            if(this.biunique_id != 'eb783144cf9e47f999708a67d48b576d') {
              names = names.split('')  // 将a字符串转换成数组
              names.splice(1, 1, '*') // 将1这个位置的字符，替换成'xxxxx'. 用的是原生js的splice方法。
              names = names.join('')  // 将数组转换成字符串。  完成。
            }
            this.studentList[stu].name = names
            if (this.studentList[stu].sex == 'woman') {
              this.studentList[stu].sex = '女'
            } else if (this.studentList[stu].sex == 'man') {
              this.studentList[stu].sex = '男'
            } else {
              this.studentList[stu].sex = ''
            }
          }
        }
      })
    },
    goshaixuan () {
      this.studentList = []
    },
    goOneToOneDonation (id) {
      this.$router.push({path: '/oneToOneDonation', query: {id: id,event_id:this.biunique_id}})
    },
    comeBack () {
      console.log(this.$refs.screen.style.display)
      if (this.$refs.screen.style.display == 'none') {
        this.studentList = []
      } else {
        window.history.go(-1)
      }
    }
  }
}
